import urllib.request
from http.client import HTTPResponse

# # 一 ,使用urllib获取百度首页源码
# # 1. 定义一个url，
# url = "http://www.baidu.com"
# # 2. 模拟浏览器向服务器发送请求
# response: HTTPResponse = urllib.request.urlopen(url)
# print('response type ：', type(response))  # <class 'http.client.HTTPResponse'>
# print('response status code ：', response.status == response.getcode())
# print('response headers ：', response.getheaders())
#
# # 3.获取响应中的页面源码
# content = response.read()
# print('content type ：', type(content))  # <class 'bytes'>
#
# # 4. 打印数据
# print(content)  # 返回的数据格式 b'xxx' ,需要解码
# # print(content.decode('UTF-8'))
#
# #  读取5字节
# print('读取字节:', response.read(1024))
# # 读取一行
# print('读取一行:', response.readline())
# # 读取所有行
# print('读取所有行:', response.readlines())
# # 获取状态码
# print('状态码:', response.status, response.getcode())
# # 获取响应头
# print('响应头:', response.getheaders())
# # 获取url
# print('url:', response.geturl())

# # 二 , 下载
# url_page = 'http://www.baidu.com'
# # url ：  下载的url
# # file_name : 下载资源的文件名称
# urllib.request.urlretrieve(url=url_page, filename='baidu.html')
#
# url_img = 'https://image.baidu.com/search/down?tn=download&word=download&ie=utf8&fr=detail&url=https%3A%2F%2Fnimg.ws.126.net%2F%3Furl%3Dhttp%253A%252F%252Fdingyue.ws.126.net%252F2024%252F0605%252Fc46c6cd2j00sel1rj00qld000nt00tmp.jpg%26thumbnail%3D660x2147483647%26quality%3D80%26type%3Djpg&thumburl=https%3A%2F%2Fimg2.baidu.com%2Fit%2Fu%3D1604541146%2C3098576070%26fm%3D253%26fmt%3Dauto%26app%3D120%26f%3DJPEG%3Fw%3D500%26h%3D622'
# urllib.request.urlretrieve(url_img, 'lisa.jpg')
#
# # url_video = 'https://www.bilibili.com/58af56d5-17d7-401b-a827-6b66328ef602'
# # urllib.request.urlretrieve(url_video, 'video.mp4')

# #  三 , 自定义请求对象
# url = 'https://www.baidu.com'
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0'
# }
# # 请求对象的定制
# request = urllib.request.Request(url, headers=headers)
# response = urllib.request.urlopen(request)
# context = response.read().decode('utf-8')
# print(context)


# 四 , 编解码(中文,特殊字符)
# # urllib.parse.quote（）
# url = 'https://www.baidu.com/s?wd='
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36'
# }
# url = url + urllib.parse.quote('中国')
# print('编码后的url :', url)
# # 请求对象
# req = urllib.request.Request(url, headers=headers)
# response = urllib.request.urlopen(req)
# print('返回的内容 : ', response.read().decode('utf-8'))

# # urllib.parse.urlencode（）
# url = 'http://www.baidu.com/s?'
# data = {
#     'name': '小刚',
#     'sex': '男',
# }
# url = url + urllib.parse.urlencode(data)
# print('编码后的url :', url)
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like ecko) Chrome/74.0.3729.169 Safari/537.36'
# }
# request = urllib.request.Request(url=url,headers=headers,method='GET')
# response = urllib.request.urlopen(request)
# print('返回的内容 : ', response.read().decode('utf-8'))


# # 五 , Post 请求
# url = 'https://fanyi.baidu.com/v2transapi'
#
# headers = {
#     # ':authority': 'fanyi.baidu.com',
#     # ':method': 'POST',
#     # ':path': '/v2transapi',
#     # ':scheme': 'https',
#     # 'accept': '*/*',
#     # 'accept‐encoding': 'gzip, deflate, br',
#     # 'accept‐language': 'zh‐CN,zh;q=0.9',
#     # 'content‐length': '119',
#     # 'content‐type': 'application/x‐www‐form‐urlencoded; charset=UTF‐8'
#
#     'cookie': 'REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1;SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; PSTM=1537097513;'
#               'BIDUPSID=D96F9A49A8630C54630DD60CE082A55C; BAIDUID=0814C35D13AE23F5EAFA8E0B24D9B436:FG=1;'
#               'to_lang_often=%5B%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%2C%7B%22value%22 %3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%5D;'
#               'from_lang_often=%5B%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%2C%7B%22value% 22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%5D; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598;'
#               'delPer=0; H_PS_PSSID=1424_21115_29522_29519_29099_29568_28835_29220_26350; PSINO=2; locale=zh;'
#               'Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1563000604,1563334706,1565592510;'
#               'Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1565592510;'
#               'yjs_js_security_passport=2379b52646498f3b5d216e6b21c6f1c7bf00f062_1565592544_js'
# }
# data = {
#     'from': 'en',
#     'to': 'zh',
#     'query': 'hello world',
#     'transtype': 'translang',
#     'simple_means_flag': '3',
#     'sign': '26505.28605',
#     'token': '2e0f1cb44414248f3a2b49fbad28bbd5'
# }
#
# # 参数的编码
# data = urllib.parse.urlencode(data).encode('utf‐8')
# # 请求对象的定制
# request = urllib.request.Request(url=url, headers=headers, data=data)
# response = urllib.request.urlopen(request)
# # 请求之后返回的所有的数据
# content = response.read().decode('utf‐8')
# import json
#
# # loads将字符串转换为python对象
# obj = json.loads(content)
# # python对象转换为json字符串 ensure_ascii=False 忽略字符集编码
# s = json.dumps(obj, ensure_ascii=False)
# print(s)

# 六 , Handler处理器
# urllib.request.urlopen(url) 不能定制请求头
# urllib.request.Request(url,headers,data) 可以定制请求头
# Handler 定制更高级的请求头
#   HTTPHandler : 处理 HTTP 协议
#   HTTPSHandler ：处理 HTTPS 协议
#   ProxyHandler : 处理代理
#   HTTPCookieProcessor : 处理 Cookie
#   HTTPRedirectHandler : 处理重定向
#   CacheFTPHandler ： 处理 FTP 协议
url = 'http://www.baidu.com'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like ecko) Chrome/74.0.3729.169 Safari/537.36'
}

request = urllib.request.Request(url=url, headers=headers)
# 处理 HTTP/HTTPS 协议
handler = urllib.request.HTTPHandler()
# 组合处理器
opener = urllib.request.build_opener(handler)
response = opener.open(request)
print('响应结果 : ', response.read().decode('utf‐8'))


# 自定义 Handler
class CustomHandler(urllib.request.BaseHandler):
    def http_request(self, req):
        """修改请求前的处理逻辑"""
        req.add_header('X-Custom-Header', 'Python')
        return req

    def http_response(self, req, response):
        """处理响应后的逻辑"""
        print(f"Response Code: {response.status}")
        return response


# 使用自定义 Handler
opener = urllib.request.build_opener(CustomHandler())
response = opener.open('http://httpbin.org/headers')
print('响应结果 : ', response.read().decode())
